package com.zlf.controller;

import com.zlf.feign.FoodFeignClient;
import com.zlf.po.Business;
import com.zlf.po.CommonResult;
import com.zlf.service.BusinessService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;


@RestController
@RefreshScope  //开启动态刷新
@RequestMapping("/BusinessController")
public class BusinessController {
    @Autowired
    private BusinessService businessService;
    @Qualifier("com.zlf.feign.FoodFeignClient")     //暂时根据提示这样解决报错
    @Autowired
    private FoodFeignClient foodFeignClient;

    @GetMapping("/listBusinessByOrderTypeId/{orderTypeId}")
    public CommonResult<List> listBusinessByOrderTypeId(
            @PathVariable("orderTypeId") Integer orderTypeId){
        List<Business> list = businessService.listBusinessByOrderTypeId(orderTypeId);
        return new CommonResult(200,"success",list);
    }

    @GetMapping("/getBusinessById/{businessId}")
    public CommonResult<Business> getBusinessById(
            @PathVariable("businessId") Integer businessId){

        Business business = businessService.getBusinessById(businessId);
        //在商家微服务中调用食品微服务
        CommonResult<List> result = foodFeignClient.listFoodByBusinessId(businessId);
        //如果食品微服务返回降级响应，那么就返回空集合
        if (result.getCode() == 200) {
            business.setFoodList(result.getResult());
        }else {
            business.setFoodList(new ArrayList());
        }
        return new CommonResult(200,"success",business);
    }
    @GetMapping("/listBusiness")
    public List<Business> listBusiness(){
        return businessService.listBusiness();
    }
    @GetMapping("/selectBusiness")
    public List<Business> selectBusiness(String key){
        return businessService.selectBusiness(key);
    }
    @GetMapping("/login/{businessId}/{pass}")
    public CommonResult login(@PathVariable("businessId") int businessId,
                              @PathVariable("pass") String pass){
        Business res;
        try {
            res=businessService.login(businessId,pass);
        }catch (Exception e){
            return new CommonResult(500,e.getMessage(),null);
        }
        return new CommonResult(200,"success",res);
    }
    @PostMapping("/saveBusiness/{businessId}/{pass}/{businessName}/{orderTypeId}/{businessAddress}")
    public CommonResult<Integer> saveBusiness(
            @PathVariable("businessId") int businessId,
            @PathVariable("pass") String pass,
            @PathVariable("businessName") String businessName,
            @PathVariable("orderTypeId") Integer orderTypeId,
            @PathVariable("businessAddress") String businessAddress){
        Business param = new Business();
        param.setBusinessId(businessId);
        param.setPass(pass);
        param.setBusinessName(businessName);
        param.setOrderTypeId(orderTypeId);
        param.setBusinessAddress(businessAddress);
        param.setBusinessExplain("新注册商家");
        param.setBusinessImg("" +
                "DhAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAJcEhZcwAAF" +
                "iUAABYlAUlSJPAAAANYSURBVGhD7ZffK2xRFMfvX6PMFE1pJj8KMQopJbxMSnmlEFLkRx7" +
                "kSUoRj5ineSAPiCfkQR7kR4oHD36FlF8R8r191z177hhz7zhnn8lJ51u7M2ftObU+a" +
                "6+19t6/8APkQjhFLoRT5EI4RS6EU6QN8f7+jre3N+Pte6QNcXV1hdLSUrS1teHl5cWwfl3Pz8/" +
                "Y3NzE8PAw7u7uDKs5aUPs7+8jEAhgYGDAsCTW7e0tzs/Psbq6iunpaTQ3N6OwsBDp" +
                "6elIS0uTMTQ0ZGlVtSEWFxfFgbm5OcPyV5eXlyguLo46GT9ycnJQX1+PcDiMk5MTSU0r0obgCmRm" +
                "ZmJiYgILCwvRsbOzg9PTU4EoKCjAzMyM2Le3t3F9fW1rHWlBMEWqq6sTRjkYDAoIIer" +
                "q6vD4+Gh8Zb+0IHZ3d5GVlYWuri5JHQ7a8vPz0dnZiYuLC+dDjIyMwOv1Yn193bAAKysr8Hg88lQ1" +
                "kQxCt01bhmBrLS8vR21tbbQ10pHW1laxcz5ZYceO+GCYkWUIFirb49TUlPyurKzE2toa2t" +
                "vbMTY2Jv9REHl5eTLPBtDS0oLu7u4Pg/WTm5uLo6Mj+c6sLEPQUUb87OxMOhNrg/UQq9h0Wlpags/n+xR" +
                "t1RxCoRAeHh4MqzlZhmAe39zcyO9kEE1NTdJuGfH4TVE1h2Sb5f+kVdhKySA6Ojo+1YtSou" +
                "ZgVimFiI+y6lzz8/Pynqg5WFFKIRhdRpnzlHK6oaEBT09PH5qDjlIKwfMU2ydXQIn/Jdjk5CRKSkrkBMzm" +
                "oKOUQKiNi2kUD0eH6TjhuAqzs7PGjHXZDnFwcIDGxkYcHx9La62oqIh2MYpdbXBwUCB02mqsbIX" +
                "o6emRvaCqqkpSKDs7W85Q6ojNZyQSQUZGhkDwyXpQ81ZlCwTThk5x8IbHcxLBmC6qE72+vmJ0dFRsRUVFs" +
                "vmVlZXZAqINwfsBI07n2PPprOpCak84PDxETU2NQNKmjhd7e3sCxG97e3txf38vdrPSgmAB9/f3SzSZJ" +
                "owmx/j4uDjMowkHneTgphfvKG906k7i9/uxvLxszHxd2ivBW9rW1pbx9keMPA+CnONKsNA3Njb+mTLcM5h+f" +
                "X19spJmZUtNfLdcCKfIhXCKXAinyIVwilwIZwj4DQkrGjqU3iA+AAAAAElFTkSuQmCC");
        param.setStarPrice(20);
        param.setDeliveryPrice(5);
        int result;
        try {
            result = businessService.saveBusiness(param);
        }catch (Exception e){
            return new CommonResult(500,e.getMessage(),null);
        }
        return new CommonResult(200,"success",result);
    }
    @PostMapping("/updateBusiness/{businessId}/{businessName}/{businessAddress}/{businessExplain}/{starPrice}/{deliveryPrice}")
    public CommonResult<Integer> updateBusiness(
            @PathVariable("businessId") int businessId,
            @PathVariable("businessName") String businessName,
            @PathVariable("businessAddress") String businessAddress,
            @PathVariable("businessExplain") String businessExplain,
            @PathVariable("starPrice") double starPrice,
            @PathVariable("deliveryPrice") double deliveryPrice){
        Business business = new Business();
        business.setBusinessId(businessId);
        business.setBusinessName(businessName);
        business.setBusinessAddress(businessAddress);
        business.setBusinessExplain(businessExplain);
        business.setStarPrice(starPrice);
        business.setDeliveryPrice(deliveryPrice);
        int result = businessService.updateBusiness(business);
        return new CommonResult(200,"success",result);
    }
}